import {
	refreshUserToken,
	refreshMeterToken
} from '@/api/currency.js'

let promise;
export function refreshTokenFun() {

	if (promise) {
		return promise
	}

	// new
	promise = new Promise(async (resolve, reject) => {
		if (uni.getStorageSync('loginStatus') == 'worker') {
			const resp = await refreshMeterToken({
				refresh_token: uni.getStorageSync('refresh_token').token
			})
			// 储存token
			uni.setStorageSync('jwt_token', resp.data.jwt);
			uni.setStorageSync('refresh_token', resp.data.refresh_token);

			// 请求成功
			resolve(resp?.code == 200);
		} else if (uni.getStorageSync('loginStatus') == 'user') {
			const resp = await refreshUserToken({
				refresh_token: uni.getStorageSync('refresh_token').token
			})
			// 储存token
			uni.setStorageSync('jwt_token', resp.data.jwt);
			uni.setStorageSync('refresh_token', resp.data.refresh_token);

			// 请求成功
			resolve(resp?.code == 200);
		} else {
			resolve(false);
		}

	})

	promise.finally(() => {
		promise = null;
	})

	return promise;
}